/**
 * 
 */
package any.tag.gwt.client.ui;

import any.gwt.client.http.AsynClientEvent;
import any.gwt.client.http.AsynClientListener;
import any.tag.gwt.client.TagClient;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

/**
 * TODO: Doc me
 * 
 * @author keke <keke@codehaus.org>
 * @version
 * @revision $Revision:$
 */
public class TagListView extends Composite implements AsynClientListener {
    private final FlowPanel outer;
    private final TagClient tagClient;
    public TagListView(final TagClient tagClient) {
        super();
        this.tagClient = tagClient;
        outer = new FlowPanel();
        initWidget(outer);
        setStyleName("view");
        addStyleName("tagList");
    }

    public void fireEvent(final AsynClientEvent event) {
        if (event.getName().equals("tags")) {
            updateView(JSONParser.parse(event.getResponse().getText())
                    .isArray());
        }

    }

    private void updateView(final JSONArray array) {
        outer.clear();
        for (int i = 0; i < array.size(); i++) {
            outer.add(new Label(array.get(i).isObject().get("about").isObject()
                    .get("$").isString().stringValue()));
        }
    }

    protected void onLoad() {
        super.onLoad();
        tagClient.addClientListener(this);
    }

    protected void onUnload() {
        super.onUnload();
        tagClient.removeClientListener(this);
    }
}
